package com.healthshare.activity.news;

import com.healthshare.R;
import com.healthshare.busi.news.TitleListBusi;
import com.healthshare.common.modal.Favorite;
import com.healthshare.parse.news.TitleListParse.TitleListItem;
import com.healthshare.tool.GlobalData;
import com.palmedia.framework.ui.CustomListView;
import com.palmedia.framework.ui.CustomMessageShow;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;



public class FavoriteActivity extends Activity
{
	
	
    private CustomListView customListView;
	 @Override
	 public void onCreate(Bundle savedInstanceState) 
	 {
		 
		 super.onCreate(savedInstanceState);
		
		
		 setContentView(R.layout.favoritelist);
		
		 customListView = (CustomListView) findViewById(R.id.list);
		
		 //customListView.setonGetMoreListener(this);
		
		 //customListView.setonRefreshListener(this);
		
		 customListView.setMoreTextColor(Color.parseColor("#668B8B"));
		
		 customListView.setUpdateTextColor(Color.parseColor("#668B8B"));
		
		 customListView.setHeadBackgrondColor(Color.parseColor("#DCDCDC"));
		
		 customListView.setBottomBackgrondColor(Color.parseColor("#DCDCDC"));
		
		((Button) this.findViewById(R.id.backbutton)).setVisibility(View.VISIBLE);
		((Button) this.findViewById(R.id.backbutton)).setTextColor(Color.rgb(0,139, 0));
		
		TextView titlecenter = (TextView) findViewById(R.id.titletextview);
		titlecenter.setText("收藏");
		
		customListView.setPageSize(TitleListBusi.pagesize);
		//customListView.freshData();
		customListView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3)
			{
				Bundle bundle=new Bundle();
				Intent intent=new Intent();
				intent.putExtra("articlebundle",bundle);
				Favorite nli = (Favorite)v.getTag();
				
				bundle.putString("articleid",nli.acticleid);
			   	intent.setClass(FavoriteActivity.this,ActicleActivity.class);
			   	startActivity(intent);
			}
    		
    	});
		customListView.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				
				final View index = view;
				final Favorite f =(Favorite)view.getTag();
				
				CustomMessageShow.getInst().showConfirmDialog(FavoriteActivity.this,"确定删除收藏："+f.title,new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						if( which==DialogInterface.BUTTON_POSITIVE)
						{
							boolean ret = GlobalData.favoriterecs.remove(f);
							boolean ret1=GlobalData.getInst().writeFavorite();
							if(ret&&ret1)
							{
								customListView.removeView(index);
								CustomMessageShow.getInst().showShortToast(FavoriteActivity.this, "删除成功");
							}
							else
							{
								CustomMessageShow.getInst().showShortToast(FavoriteActivity.this, "删除失败");
							}
							
						}
						
					}});
				return true;
			}

			/*@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3)
			{
				Bundle bundle=new Bundle();
				Intent intent=new Intent();
				intent.putExtra("bundle",bundle);
				Favorite nli = (Favorite)v.getTag();
				
				bundle.putString("acticleid",nli.acticleid);
			   	intent.setClass(FavoriteActivity.this,ActicleActivity.class);
			   	startActivity(intent);
			}*/
    		
    	});
		
		initListUI();
	 }
	 
	
	 //回调刷新界面
		
	 
	 
	 
	 
	private void initListUI()
    {
		
		/*
		 * Favorite f = new Favorite(); f.acticleid=acticleid;
		 * f.title=tlb.title;
		 */
		int size = GlobalData.favoriterecs.size();
		
		for (int i = 0; i < size; i++)
		{
			Favorite f = GlobalData.favoriterecs.get(i);
			View v = buildRowView(f);
			customListView.addViewToLast(v);
		}
		customListView.onRefreshComplete();
	}
	private View buildRowView(Favorite tt)
	{
		View view = View.inflate(this, R.layout.acticlelistrow, null);
		TextView title = (TextView)view.findViewById(R.id.title);
		TextView time = (TextView)view.findViewById(R.id.time);
		title.setText(tt.title);
		time.setText(tt.time);
		title.setTextColor(Color.parseColor("#8968CD"));
		time.setTextColor(Color.parseColor("#8968CD"));
		view.setTag(tt);
	
		
		return view;
	}
	public void backButtonAction(View view)
	{
		this.finish();
	}
}